$(document).ready(function () {
    var province = $("#province");
    var city = $("#city");
    var district = $("#district");
    city.css("disabled", "true");
    district.css("disabled", "true");
    var oldCityName = city.val();
    var oldDistrictName = district.val();
    var sUrl = "/static/scripts/address/area.js";
    var provinces = {};
    var cities = {};
    var counties = {};

    var initAddress = function () {        
        $.ajax({
        	type : "get",
        	async : false,
        	url : sUrl,
            contentType : "application/json; charset=utf-8",
            dataType : "json",
            cache : true,        	
        	success : function(data) {
            	for (var i = 0; i < data.length; i++) {
                	provinces[data[i].chinese] = data[i];
                }
        	}
        });        
    };
    
    var clearSelectOptions = function(selectoId) {
	    var e = document.getElementById(selectoId);
	    if (e == null) {
	        return true;
	    }
	    var length = e.options.length;
	    for (var i = 0; i < length; i++) {
	        e.options[0] = null;
	    }
	    return true;
	};

    var loadCity = function () {
        var provinceName = province.val();
        if (!provinces.hasOwnProperty(provinceName)) {
            clearCity();
        } else {
            var province_ = provinces[provinceName];
            var result = province_.cities;
            for (var i = 0; i < result.length; i++) {
            	if(oldCityName !== result[i].cityname) {
            		city.append("<option value='" + result[i].cityname + "'>" + result[i].cityname + "</option>");
            	}                
                cities[result[i].cityname] = result[i];
            }
        }
        loadCounty();
    };

    var loadCounty = function () {
        var cityName = city.val();
        if (cities.hasOwnProperty(cityName)) {
            var city_ = cities[cityName];
            var result = city_.counties;
            for (var i = 0; i < result.length; i++) {
            	if(oldDistrictName !== result[i].countyname) {
            		district.append("<option value='" + result[i].countyname + "'>" + result[i].countyname + "</option>");
            	}                
                counties[result[i].countyname] = result[i];
            }
        } else {
            clearCounty();
        }
    };
	
	var changeCounty = function() {
		var cityName = city.val();
        if (cities.hasOwnProperty(cityName)) {
            var city_ = cities[cityName];
            var result = city_.counties;
			clearCounty();
            for (var i = 0; i < result.length; i++) {            	
            	district.append("<option value='" + result[i].countyname + "'>" + result[i].countyname + "</option>");            	                
                counties[result[i].countyname] = result[i];
            }
        } else {
            clearCounty();
        }		
	}

    var clearCity = function () {
        var e = document.getElementById("city");
        if (e == null) {
            return true;
        }
        e.length = 1;
        e.options[0].text = '请选择城市';
        e.options[0].value = '';
        return true;
    };

    var clearCounty = function () {
        var e = document.getElementById("district");
        if (e == null) {
            return true;
        }
        e.length = 1;
        e.options[0].text = '请选则区县';
        e.options[0].value = '';
        return true;
    };

    $("#city").change(changeCounty);

    initAddress();
	
	loadCity();
});
